//
//  ConditionViewController.m
//  CwinCar
//
//  Created by dlios on 15/10/24.
//  Copyright © 2015年 dlios. All rights reserved.
//

#import "ConditionViewController.h"
#import "AppDelegate.h"
#import "ConiditionDetailViewController.h"
@interface ConditionViewController ()<UITextFieldDelegate>
{
    UITextField *highcondition;
    UITextField *condition;
}
@end

@implementation ConditionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    _zeroArray = [[NSMutableArray alloc]init];
    _oneArray = [[NSMutableArray alloc]init];
    _threeArray = [[NSMutableArray alloc]init];
    _twoArray = [[NSMutableArray alloc]init];
    _fiveArray = [[NSMutableArray alloc]init];
    _fourArray = [[NSMutableArray alloc]init];
    _priceArray = [[NSMutableArray alloc]init];
    
    
    self.navigationItem.title = @"条件找车";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(search)];
    
 
//    http://mi.xcar.com.cn/interface/xcarapp/getCarByCondition.php?config=&displacement=&frame=&level=&limit=20&nation=&offset=0&price=&sortType=1&transmition=
    [self creatScroll];

}

//点击搜索
-(void)search
{
    ConiditionDetailViewController *web = [[ConiditionDetailViewController alloc]init];
    web.hidesBottomBarWhenPushed = YES;
    NSString *zero = @"";
    NSString *one = @"";
    NSString *two = @"";
    NSString *three = @"";
    NSString *four = @"";
    NSString *five = @"";
    if (_zeroArray.count != 0 ) {
        //进行排序,用户的点击顺序不一定是正序
        NSArray *arr = [_zeroArray sortedArrayUsingSelector:@selector(compare:)];
        //拼接字符串
        for (int i = 0; i< arr.count; i++) {
            if (i == arr.count - 1) {
                zero = [zero stringByAppendingString:arr[i]];
            }
            else{
            zero = [zero stringByAppendingString:[NSString stringWithFormat:@"%@_", arr[i]]];
            }
        }
    }
    else{
        zero = @"";
    }
    if (_oneArray.count != 0) {
        NSArray *arr = [_oneArray sortedArrayUsingSelector:@selector(compare:)];
    
        for (int i = 0; i< arr.count; i++) {
            
            if (i == arr.count - 1) {
                one = [one stringByAppendingString:arr[i]];
            }
            else{
            one = [one stringByAppendingString:[NSString stringWithFormat:@"%@_", arr[i]]];
            }
        }
    }
    else{
        one = @"";
    }
    
    if (_twoArray.count != 0) {
        NSArray *arr = [_twoArray sortedArrayUsingSelector:@selector(compare:)];
        
        for (int i = 0; i< arr.count; i++) {
            if (i == arr.count - 1) {
                two = [two stringByAppendingString:arr[i]];
            }
            else{
            two = [two stringByAppendingString:[NSString stringWithFormat:@"%@_", arr[i]]];
            }
        }
    }
    else{
        two = @"";
    }
    if (_threeArray.count != 0) {
        NSArray *arr = [_threeArray sortedArrayUsingSelector:@selector(compare:)];
        
        for (int i = 0; i< arr.count; i++) {
            if (i == arr.count - 1) {
                three = [three stringByAppendingString:arr[i]];
            }
            else{
            three = [three stringByAppendingString:[NSString stringWithFormat:@"%@_", arr[i]]];
            }
        }
    }
    else{
        three = @"";
    }
    if (_fourArray.count != 0) {
        NSArray *arr = [_fourArray sortedArrayUsingSelector:@selector(compare:)];
        
        for (int i = 0; i< arr.count; i++) {
            if (i == arr.count - 1) {
                four = [four stringByAppendingString:arr[i]];
            }
            else{
            four = [four stringByAppendingString:[NSString stringWithFormat:@"%@_", arr[i]]];
            }
        }
    }
    else{
        four = @"";
    }
    if (_fiveArray.count != 0) {
        NSArray *arr = [_fiveArray sortedArrayUsingSelector:@selector(compare:)];
        
        for (int i = 0; i< arr.count; i++) {
            if (i == arr.count - 1) {
                five = [five stringByAppendingString:arr[i]];
            }
            else{
            five = [five stringByAppendingString:[NSString stringWithFormat:@"%@_", arr[i]]];
            }
        }
    }
    else{
        five = @"";
    }

    NSString *price = @"";
    if (_priceArray.count > 1 ) {
        
        NSString *dijia = _priceArray[0];
        NSString *gaojia = _priceArray[1];
       
            if (dijia.integerValue >= 0 && dijia.integerValue <= 4 ) {
                dijia = @"1";
            }
            if (dijia.integerValue >= 5 && dijia.integerValue <= 8 ) {
                dijia = @"2";
            }
            if (dijia.integerValue >= 9 && dijia.integerValue <= 12 ) {
                dijia = @"3";
            }
            if (dijia.integerValue >= 13 && dijia.integerValue <= 18 ) {
                dijia = @"4";
            }
            if (dijia.integerValue >= 19 && dijia.integerValue <= 25 ) {
                dijia = @"5";
            }
            if (dijia.integerValue >= 26 && dijia.integerValue <= 35) {
                dijia = @"6";
            }
            if (dijia.integerValue >= 36 && dijia.integerValue <= 50) {
                dijia = @"7";
            }
            if (dijia.integerValue >= 51 && dijia.integerValue <= 70 ) {
                dijia = @"8";
            }
            if (dijia.integerValue >70) {
                dijia = @"9";
            }
            
            
            if (gaojia.integerValue >= 0 && gaojia.integerValue <= 4 ) {
                gaojia = @"1";
            }
            if (gaojia.integerValue >= 5 && gaojia.integerValue <= 8 ) {
                gaojia = @"2";
            }
            if (gaojia.integerValue >= 9 && gaojia.integerValue <= 12 ) {
                gaojia = @"3";
            }
            if (gaojia.integerValue >= 13 && gaojia.integerValue <= 18 ) {
                gaojia = @"4";
            }
            if (gaojia.integerValue >= 19 && gaojia.integerValue <= 25 ) {
                gaojia = @"5";
            }
            if (gaojia.integerValue >= 26 && gaojia.integerValue <= 35) {
                gaojia = @"6";
            }
            if (gaojia.integerValue >= 36 && gaojia.integerValue <= 50) {
                gaojia = @"7";
            }
            if (gaojia.integerValue >= 51 && gaojia.integerValue <= 70 ) {
                gaojia = @"8";
            }
            if (gaojia.integerValue >70) {
                gaojia = @"9";
            }
            
            if (gaojia.integerValue == dijia.integerValue) {
                price = gaojia;
            }
            else if (dijia.integerValue == 1 && gaojia.integerValue == 9) {
                price = @"";
            }
            else {
                for (int i = dijia.intValue; i < gaojia.intValue+1; i++) {
                    if (i == gaojia.integerValue) {
                        price = [price stringByAppendingString:[NSString stringWithFormat:@"%d",i]];
                    }
                    else{
                    price = [price stringByAppendingString:[NSString stringWithFormat:@"%d_",i]];
                }
                }
            }
        
    }

    
    if (_priceArray.count == 1) {
        price = _priceArray[0];
    }
    
    web.webStr = [NSString stringWithFormat:@"http://mi.xcar.com.cn/interface/xcarapp/getCarByCondition.php?config=%@&displacement=%@&frame=%@&level=%@&limit=20&nation=%@&offset=0&price=%@&sortType=1&transmition=%@",five,two,three,zero,one,price,four];
    
//    NSLog(@"aodwoiaojwdjadowj     %@",web.webStr);
    
    
    
    
    
    [self.navigationController pushViewController:web animated:YES];
}
-(void)creatScroll
{
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width, mainScree_Height-44)];
    self.scroll.contentSize = CGSizeMake(0, mainScree_Height*2.5 );
    [self.view addSubview:self.scroll];
    [self creatLabel];
    [self creatLevelButton];
    [self creatCountry];
    [self creatCapacity];
    [self creatStructure];
    [self creatAMT];
    [self creatConfiguration];
}
//价格范围(手动填写)
-(void)creatLabel
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10*suitableScreen_Height, mainScreen_Width/5, 25*suitableScreen_Height)];
    label.text = @"价格区间";
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
    [label setTextColor:[UIColor whiteColor]];
    [self.scroll addSubview:label];
    
    condition = [[UITextField alloc]initWithFrame:CGRectMake(10+mainScreen_Width/6, 40*suitableScreen_Height, mainScreen_Width/6, 30*suitableScreen_Height)];
    condition.layer.borderWidth = 1;
    condition.layer.cornerRadius = 5;
    condition.placeholder = @" 最低";
    condition.delegate = self;
    condition.textAlignment = NSTextAlignmentCenter;
    condition.layer.borderColor = [UIColor colorWithRed:0.71 green:0.71 blue:0.71 alpha:.5].CGColor;

    [self.scroll addSubview:condition];
    
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(mainScreen_Width/3+20, 40*suitableScreen_Height, mainScreen_Width/6, 30*suitableScreen_Height)];
    lab.text = @" - ";
    lab.textAlignment = NSTextAlignmentCenter;
    [self.scroll addSubview:lab];
    
    highcondition = [[UITextField alloc]initWithFrame:CGRectMake(30+mainScreen_Width/2, 40*suitableScreen_Height, mainScreen_Width/6, 30*suitableScreen_Height)];
    highcondition.delegate = self;
    highcondition.layer.borderWidth = 1;
    highcondition.layer.cornerRadius = 5;
    highcondition.layer.borderColor = [UIColor colorWithRed:0.71 green:0.71 blue:0.71 alpha:.5].CGColor;
    highcondition.placeholder = @" 最高";
    highcondition.textAlignment = NSTextAlignmentCenter;
    [self.scroll addSubview:highcondition];
    
    UILabel *danwei = [[UILabel alloc]initWithFrame:CGRectMake(mainScreen_Width*2/3+40, 40*suitableScreen_Height, mainScreen_Width/6, 30*suitableScreen_Height)];
    danwei.text = @"万(元)";
    [self.scroll addSubview:danwei];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handletapPressGesture:)];
    tapGesture.cancelsTouchesInView = YES;
    [self.view addGestureRecognizer:tapGesture];

    [_priceArray removeAllObjects];
    return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    //输入完成如果两个textField都不为空且都是数字
    if (![condition.text isEqualToString:@""] &&![highcondition.text isEqualToString:@""] && [self isNumber:textField.text] ) {
        
            if (highcondition.text.intValue > condition.text.intValue) {
                
                [_priceArray addObject:condition.text];
                [_priceArray addObject:highcondition.text];
            }
            
            else
            {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"最高价格应大于最低价格,请修改输入" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
        
    }
    //输入都为空
    if ([condition.text isEqualToString:@""] && [highcondition.text isEqualToString:@""]) {
        [_priceArray addObject:@""];
    }
    //如果输入的不是数字
    if (![self isNumber:textField.text]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入数字" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
    
//    NSLog(@"%@", _priceArray);
    return YES;
}
//判断输入的是否是数字
-(BOOL)isNumber:(NSString *)str
{
    int count = 0;
    for (int i = 0; i < str.length; i++) {
        if ([str characterAtIndex:i] >= '0' && [str characterAtIndex:i] <= '9') {
            count ++;
        }
    }
    if (count == str.length) {
        return YES;
    }

    return NO;
}
//点击空白,隐藏按钮
-(void)handletapPressGesture:(UITapGestureRecognizer*)sender
{
    [condition resignFirstResponder];
    [highcondition resignFirstResponder];
    [self.view removeGestureRecognizer:sender];
    //如果只输入一个值
    if (([condition.text isEqualToString:@""] &&![highcondition.text isEqualToString:@""] &&[self isNumber:highcondition.text]) || (![condition.text isEqualToString:@""] &&[self isNumber:condition.text] && [highcondition.text isEqualToString:@""]) ) {
        
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入完整价格区间" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
        
        
    }
    
}
-(void)creatLevelButton
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100*suitableScreen_Height, mainScreen_Width/5, 25*suitableScreen_Height)];
    label.text = @" 级别 ";
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
    [label setTextColor:[UIColor whiteColor]];
    [self.scroll addSubview:label];
    
    NSArray *levelArray = @[@"微型车", @"小型车", @"紧凑型车", @"中型车",@"中大型车", @"豪华车",@"SUV越野",@"MPV商务",@"跑车"];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            UIButton *levelButton = [UIButton buttonWithType:UIButtonTypeCustom];
            levelButton.frame = CGRectMake(20*suitableScreen_Width+j*((mainScreen_Width-50)/3+10), (130+60*i)*suitableScreen_Height, (mainScreen_Width-50)/3, 50*suitableScreen_Height);
            [levelButton setTitle:levelArray[i*3+j] forState:UIControlStateNormal];
            levelButton.titleLabel.textAlignment = NSTextAlignmentCenter;
            levelButton.layer.borderWidth = 1;
            levelButton.layer.cornerRadius = 5;
            levelButton.tag = i*3+j+100+1;
            levelButton.layer.borderColor = [UIColor colorWithRed:0.71 green:0.71 blue:0.71 alpha:.5].CGColor;
//            levelButton.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
            [levelButton setTitleColor:[UIColor colorWithRed:0.24 green:0.24 blue:0.25 alpha:1]  forState:UIControlStateNormal];
            [levelButton addTarget:self  action:@selector(levelSelect:) forControlEvents:UIControlEventTouchUpInside];
            
            [self.scroll addSubview:levelButton];
        }
    }
}


-(void)creatCountry
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 330*suitableScreen_Height, mainScreen_Width/5, 25*suitableScreen_Height)];
    label.text = @" 国别 ";
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
    [label setTextColor:[UIColor whiteColor]];
    [self.scroll addSubview:label];
    
    NSArray *levelArray = @[@"中国🇨🇳", @"德国🇩🇪", @"日本🇯🇵", @"美国🇺🇸",@"韩国🇰🇷", @"法国🇫🇷",@"英国🇬🇧",@"意大利🇮🇹",@"其他🌐"];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            UIButton *levelButton = [UIButton buttonWithType:UIButtonTypeCustom];
            levelButton.frame = CGRectMake(20*suitableScreen_Width+j*((mainScreen_Width-50)/3+10), (360+60*i)*suitableScreen_Height, (mainScreen_Width-50)/3, 50*suitableScreen_Height);
            [levelButton setTitle:levelArray[i*3+j] forState:UIControlStateNormal];
            levelButton.titleLabel.textAlignment = NSTextAlignmentCenter;
            levelButton.layer.borderWidth = 1;
            levelButton.layer.cornerRadius = 5;
            levelButton.tag = i*3+j+200+1;
            levelButton.layer.borderColor = [UIColor colorWithRed:0.71 green:0.71 blue:0.71 alpha:.5].CGColor;
            //            levelButton.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
            [levelButton setTitleColor:[UIColor colorWithRed:0.24 green:0.24 blue:0.25 alpha:1]  forState:UIControlStateNormal];
            [levelButton addTarget:self  action:@selector(levelSelect1:) forControlEvents:UIControlEventTouchUpInside];
            [self.scroll addSubview:levelButton];
        }
    }

}
-(void)creatCapacity
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 560*suitableScreen_Height, mainScreen_Width/5, 25*suitableScreen_Height)];
    label.text = @" 排量 ";
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
    [label setTextColor:[UIColor whiteColor]];
    [self.scroll addSubview:label];
    
    NSArray *levelArray = @[@"1.0L以下", @"1.0L-1.4L", @"1.4L-1.6L", @"1.6-2.0L",@"2.0-2.5L", @"2.5L以上"];
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            UIButton *levelButton = [UIButton buttonWithType:UIButtonTypeCustom];
            levelButton.frame = CGRectMake(20*suitableScreen_Width+j*((mainScreen_Width-50)/3+10), (590+60*i)*suitableScreen_Height, (mainScreen_Width-50)/3, 50*suitableScreen_Height);
            [levelButton setTitle:levelArray[i*3+j] forState:UIControlStateNormal];
            levelButton.titleLabel.textAlignment = NSTextAlignmentCenter;
            levelButton.layer.borderWidth = 1;
            levelButton.layer.cornerRadius = 5;
            levelButton.tag = i*3+j+300+1;
            levelButton.layer.borderColor = [UIColor colorWithRed:0.71 green:0.71 blue:0.71 alpha:.5].CGColor;
            //            levelButton.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
            [levelButton setTitleColor:[UIColor colorWithRed:0.24 green:0.24 blue:0.25 alpha:1]  forState:UIControlStateNormal];
            [levelButton addTarget:self  action:@selector(levelSelect2:) forControlEvents:UIControlEventTouchUpInside];
            [self.scroll addSubview:levelButton];
        }
    }
}
-(void)creatStructure
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 730*suitableScreen_Height, mainScreen_Width/5, 25*suitableScreen_Height)];
    label.text = @" 结构 ";
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
    [label setTextColor:[UIColor whiteColor]];
    [self.scroll addSubview:label];
    
    NSArray *levelArray = @[@"两厢", @"三厢"];
    for (int i = 0; i < 1; i++) {
        for (int j = 0; j < 2; j++) {
            UIButton *levelButton = [UIButton buttonWithType:UIButtonTypeCustom];
            levelButton.frame = CGRectMake(20*suitableScreen_Width+j*((mainScreen_Width-50)/3+10), (760+60*i)*suitableScreen_Height, (mainScreen_Width-50)/3, 50*suitableScreen_Height);
            [levelButton setTitle:levelArray[i*3+j] forState:UIControlStateNormal];
            levelButton.titleLabel.textAlignment = NSTextAlignmentCenter;
            levelButton.layer.borderWidth = 1;
            levelButton.layer.cornerRadius = 5;
            levelButton.tag = i*3+j+400+1;
            levelButton.layer.borderColor = [UIColor colorWithRed:0.71 green:0.71 blue:0.71 alpha:.5].CGColor;
            //            levelButton.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
            [levelButton setTitleColor:[UIColor colorWithRed:0.24 green:0.24 blue:0.25 alpha:1]  forState:UIControlStateNormal];
            [levelButton addTarget:self  action:@selector(levelSelect3:) forControlEvents:UIControlEventTouchUpInside];
            [self.scroll addSubview:levelButton];
        }
    }
}
-(void)creatAMT
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 840*suitableScreen_Height, mainScreen_Width/5, 25*suitableScreen_Height)];
    label.text = @" 变速箱 ";
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
    [label setTextColor:[UIColor whiteColor]];
    [self.scroll addSubview:label];
    
    NSArray *levelArray = @[@"手动", @"自动"];
    for (int i = 0; i < 1; i++) {
        for (int j = 0; j < 2; j++) {
            UIButton *levelButton = [UIButton buttonWithType:UIButtonTypeCustom];
            levelButton.frame = CGRectMake(20*suitableScreen_Width+j*((mainScreen_Width-50)/3+10), (870+60*i)*suitableScreen_Height, (mainScreen_Width-50)/3, 50*suitableScreen_Height);
            [levelButton setTitle:levelArray[i*3+j] forState:UIControlStateNormal];
            levelButton.titleLabel.textAlignment = NSTextAlignmentCenter;
            levelButton.layer.borderWidth = 1;
            levelButton.layer.cornerRadius = 5;
            levelButton.tag = i*3+j+500+1;
            levelButton.layer.borderColor = [UIColor colorWithRed:0.71 green:0.71 blue:0.71 alpha:.5].CGColor;
            //            levelButton.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
            [levelButton setTitleColor:[UIColor colorWithRed:0.24 green:0.24 blue:0.25 alpha:1]  forState:UIControlStateNormal];
            [levelButton addTarget:self  action:@selector(levelSelect4:) forControlEvents:UIControlEventTouchUpInside];
            [self.scroll addSubview:levelButton];
        }
    }
}
-(void)creatConfiguration
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 950*suitableScreen_Height, mainScreen_Width/5, 25*suitableScreen_Height)];
    label.text = @"心动配置";
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
    [label setTextColor:[UIColor whiteColor]];
    [self.scroll addSubview:label];

    NSArray *array = @[@"ISO FIX儿童座椅接口",@"LATCH儿童座椅接口",@"前雾灯",@"定速巡航",@"无钥匙启动系统",@"天窗",@"多功能方向盘",@"车身稳定控制(ESP/DSC等)",@"倒车视频影像",@"自动泊车入位",@"第三排座椅",@"前排座椅加热",@"真皮/仿皮座椅",@"日间行车灯",@"中控台彩色大屏",@"副驾驶座电动调节",@"8喇叭以上扬声器系统",@"氙气大灯",@"GPS导航系统",@"自动空调",@"后排出风口",@"外接音源接口(AUX/USB等)"];
    
    for (int i = 0; i < 11; i++) {
        for (int j = 0; j < 2; j++) {
            UIButton *levelButton = [UIButton buttonWithType:UIButtonTypeCustom];
            levelButton.frame = CGRectMake(20*suitableScreen_Width+j*((mainScreen_Width-50)/2+10), (980+60*i)*suitableScreen_Height, (mainScreen_Width-50)/2, 50*suitableScreen_Height);
            [levelButton setTitle:array[i*2+j] forState:UIControlStateNormal];
            levelButton.titleLabel.textAlignment = NSTextAlignmentCenter;
            levelButton.layer.borderWidth = 1;
            levelButton.layer.cornerRadius = 5;
            levelButton.tag = i*3+j+600+1;
            levelButton.layer.borderColor = [UIColor colorWithRed:0.71 green:0.71 blue:0.71 alpha:.5].CGColor;
            //            levelButton.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
            [levelButton setTitleColor:[UIColor colorWithRed:0.24 green:0.24 blue:0.25 alpha:1]  forState:UIControlStateNormal];
            [levelButton addTarget:self  action:@selector(levelSelect5:) forControlEvents:UIControlEventTouchUpInside];
            [self.scroll addSubview:levelButton];
        }
    }

}

-(void)levelSelect:(UIButton *)sender
{
    
    
    if (!sender.selected) {
        sender.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
        NSInteger zero = sender.tag % 100;
        [_zeroArray addObject:[NSString stringWithFormat:@"%ld",zero]];
        sender.selected = YES;
    }
    else {
        sender.backgroundColor = [UIColor whiteColor];
        sender.selected = NO;
        
        for (int i = 0; i < _zeroArray.count; i++) {
            if ([_zeroArray[i] isEqualToString:[NSString stringWithFormat:@"%ld",sender.tag % 100]]) {
                  [_zeroArray removeObject:_zeroArray[i]];
            }
        }
    
    }
    
}

-(void)levelSelect1:(UIButton *)sender
{
    
    if (!sender.selected) {
        sender.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
        NSInteger zero = sender.tag % 200;
        [_oneArray addObject:[NSString stringWithFormat:@"%ld",zero]];
        sender.selected = YES;
    }
    else {
        sender.backgroundColor = [UIColor whiteColor];
        sender.selected = NO;
        for (int i = 0; i < _oneArray.count; i++) {
            if ([_oneArray[i] isEqualToString:[NSString stringWithFormat:@"%ld",sender.tag % 200]]) {
                [_oneArray removeObject:_oneArray[i]];
            }
        }
    }
    
}

-(void)levelSelect2:(UIButton *)sender
{
    
    
    if (!sender.selected) {
        sender.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
        NSInteger zero = sender.tag % 300;
        [_twoArray addObject:[NSString stringWithFormat:@"%ld",zero]];
        sender.selected = YES;
    }
    else {
        sender.backgroundColor = [UIColor whiteColor];
        sender.selected = NO;
        
        for (int i = 0; i < _twoArray.count; i++) {
            if ([_twoArray[i] isEqualToString:[NSString stringWithFormat:@"%ld",sender.tag % 300]]) {
                [_twoArray removeObject:_twoArray[i]];
            }
        }
    }

    
}

-(void)levelSelect3:(UIButton *)sender
{
    
    
    if (!sender.selected) {
        sender.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
        NSInteger zero = sender.tag % 400;
        [_threeArray addObject:[NSString stringWithFormat:@"%ld",zero]];
        sender.selected = YES;
    }
    else {
        sender.backgroundColor = [UIColor whiteColor];
        sender.selected = NO;
        
        for (int i = 0; i < _threeArray.count; i++) {
            if ([_threeArray[i] isEqualToString:[NSString stringWithFormat:@"%ld",sender.tag % 400]]) {
                [_threeArray removeObject:_threeArray[i]];
            }
        }

    }
    
}

-(void)levelSelect4:(UIButton *)sender
{
    
    
    if (!sender.selected) {
        sender.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
        NSInteger zero = sender.tag % 500;
        [_fourArray addObject:[NSString stringWithFormat:@"%ld",zero]];
        sender.selected = YES;
    }
    else {
        sender.backgroundColor = [UIColor whiteColor];
        sender.selected = NO;
        
        for (int i = 0; i < _fourArray.count; i++) {
            if ([_fourArray[i] isEqualToString:[NSString stringWithFormat:@"%ld",sender.tag % 500]]) {
                [_fourArray removeObject:_fourArray[i]];
            }
        }

    }
    
    
}

-(void)levelSelect5:(UIButton *)sender
{
    
    if (!sender.selected) {
        sender.backgroundColor = [UIColor colorWithRed:0.31 green:0.69 blue:0.9 alpha:1];
        NSInteger zero = sender.tag % 600;
        [_fiveArray addObject:[NSString stringWithFormat:@"%ld",zero]];
        sender.selected = YES;
    }
    else {
        sender.backgroundColor = [UIColor whiteColor];
        sender.selected = NO;
        
        for (int i = 0; i < _fiveArray.count; i++) {
            if ([_fiveArray[i] isEqualToString:[NSString stringWithFormat:@"%ld",sender.tag % 600]]) {
                [_fiveArray removeObject:_fiveArray[i]];
            }
        }

    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
